package entity

import (
	"strconv"
	"time"

	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/bi/cube"
)

//TBASECUCY 币种
type TBASECUCY struct {
	CYNO string `gorm:"primary_key;column:CYNO;size:20"`
	CYNM string `gorm:"column:CYNM;size:20"`
}

//TableName 币种名
func (TBASECUCY) TableName() string {
	return "T_BASE_CUCY"
}

//Otion 机构
type Otion struct {
	ID        int64  `gorm:"primary_key;column:ID"`
	OrganName string `gorm:"column:OrganName;size:20"`
}

//TableName 机构名称
func (Otion) TableName() string {
	return "T_Organization"
}

//GJJSL 用户
type GJJSL struct {
	JLID                   int        `gorm:"AUTO_INCREMENT;primary_key;column:JLID"`
	CurrCD                 string     `gorm:"column:CURR_CD;size:10"`
	TBASECUCY              TBASECUCY  `gorm:"ForeignKey:CurrCD"`
	OID                    int64      `gorm:"column:UTID"`
	Otion                  Otion      `gorm:"ForeignKey:OID"`
	HappDT                 *time.Time `gorm:"column:HAPP_DT;type:date;"`
	MatureDT               *time.Time `gorm:"column:MATURE_DT;type:date;"`
	CustnaME               string     `gorm:"column:CUST_NAME;size:20"`
	CommfeeincoME          float64    `gorm:"column:COMMFEE_INCOME"`
	Amt                    float64    `gorm:"column:AMT"`
	CurrbAL                float64    `gorm:"column:CURR_BAL"`
	MarginAmt              float64    `gorm:"column:MARGIN_AMT"`
	MarginCurr             string     `gorm:"column:MARGIN_CURR"`
	OrgorgCd               string     `gorm:"column:ORG_ORG_CD"`
	Glunm                  string     `gorm:"column:GL_NUM"`
	Glunm2                 string     `gorm:"column:GL_NUM2"`
	Glunmbal2              float64    `gorm:"column:CURR_BAL2"`
	Amtdistcntrmb          float64    `gorm:"column:AMT_DISCNT_RMB"`
	Currbaldiscntrmb       float64    `gorm:"column:CURR_BAL_DISCNT_RMB"`
	Commfeeincomediscntrmb float64    `gorm:"column:COMMFEE_INCOME_DISCNT_RMB"`
	Currbal2discntrmb      float64    `gorm:"column:CURR_BAL2_DISCNT_RMB"`
	Marginamtdiscntrmb     float64    `gorm:"column:MARGIN_AMT_DISCNT_RMB"`
	CUID                   int        `gorm:"column:CUID"`
	YG1                    string     `gorm:"-"`
	ZB1                    *float64   `gorm:"-"`
	YG2                    string     `gorm:"-"`
	ZB2                    *float64   `gorm:"-"`
	YG3                    string     `gorm:"-"`
	ZB3                    *float64   `gorm:"-"`
	YG4                    string     `gorm:"-"`
	ZB4                    *float64   `gorm:"-"`
	YG5                    string     `gorm:"-"`
	ZB5                    *float64   `gorm:"-"`
}

//TableName User用户表名
func (GJJSL) TableName() string {
	return "T_CM_INTER_STL_SUM_M"
}

func (c *GJJSL) AfterFind() (err error) {
	var share *cube.Share
	share, err = cube.GetShare("comp_qtzb_divi")
	if err != nil {
		return
	}
	var diviMap map[string]float64
	diviMap, err = share.GetDivision("9:" + strconv.Itoa(c.JLID))
	if err != nil {
		return
	}
	i := 0
	if diviMap == nil || len(diviMap) == 0 {
		share, err = cube.GetShare("comp_clie_divi")
		if err != nil {
			return
		}
		diviMap, err = share.GetDivision(strconv.Itoa(c.CUID))
		if err != nil {
			return
		}
	}
	for k, v := range diviMap {
		val := v
		i++
		switch i {
		case 1:
			c.YG1 = k
			c.ZB1 = &val
		case 2:
			c.YG2 = k
			c.ZB2 = &val
		case 3:
			c.YG3 = k
			c.ZB3 = &val
		case 4:
			c.YG4 = k
			c.ZB4 = &val
		case 5:
			c.YG5 = k
			c.ZB5 = &val
		default:
			break
		}
	}
	return
}

// TBASECUCYEF 返回一个机构体
func TBASECUCYEF() base.EntityFunc {
	return func() interface{} {
		entity := TBASECUCY{}
		return &entity
	}
}

// TBASECUCYESF 返回一个机构体数组
func TBASECUCYESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []TBASECUCY
		return &entities
	}
}

// OtionEF 返回一个机构体
func OtionEF() base.EntityFunc {
	return func() interface{} {
		entity := Otion{}
		return &entity
	}
}

// OtionESF 返回一个机构体数组
func OtionESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Otion
		return &entities
	}
}

// GJJSLEF G
func GJJSLEF() base.EntityFunc {
	return func() interface{} {
		entity := GJJSL{}
		return &entity
	}
}

//GJJSLESF GJ
func GJJSLESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []GJJSL
		return &entities
	}
}
func init() {
	base.DaoPoolObj.Put("TBASECUCY", base.NewDefaultDao(TBASECUCYEF(), TBASECUCYESF(), nil))
	base.DaoPoolObj.Put("Otion", base.NewDefaultDao(OtionEF(), OtionESF(), nil))
	relationship := []string{"TBASECUCY", "Otion"}
	dao := base.NewDefaultDao(GJJSLEF(), GJJSLESF(), relationship)
	filter := base.DefaultFilter{
		FilterColName: "JLID",
		TableName:     "T_COMP_QTZB_DIVI",
		EmidColName:   "emid",
		AcidColName:   "jlid",
		WhereCause:    "ywid=9",
	}
	dao.Fileter = &filter
	base.DaoPoolObj.Put("GJJSL", dao)
}
